package com.lds.config.center.server.mcp.agent.controller;

import com.lds.config.center.server.mcp.agent.agent.EnvMcpAgent;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatModelController {
    private final ChatModel chat;

    public ChatModelController(ChatModel chat, StreamingChatModel streamingChat, ToolProvider toolProvider) {
        this.chat = chat;
        this.streamingChat = streamingChat;
        this.envMcpAgent = AiServices.builder(EnvMcpAgent.class)
                .chatModel(chat)
                .toolProvider(toolProvider)
                .build();
    }

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "message", defaultValue = "你好") String message) {
        return chat.chat(message);
    }

    @GetMapping("/image")
    public String image(String imageUrl) {
        UserMessage userMessage = UserMessage.from(
                TextContent.from("Describe the following image"),
                ImageContent.from("https://example.com/cat.jpg")
        );
        ChatResponse response = chat.chat(userMessage);
        return response.aiMessage().text();
    }

    private final StreamingChatModel streamingChat;
    @GetMapping("/streaming-chat")
    public String streamingChat(@RequestParam(value = "message", defaultValue = "你好") String message) {
        streamingChat.chat(message, new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String partialResponse) {
                    System.out.println("onPartialResponse: " + partialResponse);
            }

            @Override
            public void onCompleteResponse(ChatResponse completeResponse) {
            }

            @Override
            public void onError(Throwable error) {
            }
        });
        return chat.chat(message);
    }

}